<template>
<view class="root">
  <view class="homeinfo">当前门店名称：{{storeName}}</view>
  <view class="device-connect-title">设备配网</view>
  <view>
    <view class="device-connect-item" @click="scanCodeBind">
      <view class="device-connect-item-name">扫码绑定</view>
      <van-icon size="20" name="arrow" color="#c8c8c8" />
    </view>
  </view>
</view>
</template>

<script>

import { getEquipment } from '../../api/system/equiment/equipment'
  export default {
    data() {
      return {
        storeName:null,
        storeId:null
      }
    },
    onLoad(option) {
      this.storeId=option.storeId
      this.storeName=option.storeName
    },
    methods: {
      //扫码绑定
      scanCodeBind(){
        const thant =this
        //解析mac(8)+设备类型(16)+校验(32)
          uni.scanCode({
            onlyFromCamera: false,
            success: function(res) {
            const codes = res.result
            const equipmentTypeId=codes.substring(0,8);
            const equipmentNo = codes.substring(8,24)
            const check = codes.substring(24,56)
            var hash =md5(codes.substring(0,24)).toUpperCase()
            if(hash==check){
             //先查询该用户下是否存在该设备
             getEquipment(equipmentNo).then(response => {
                 if(response.data!=null){
                    thant.$modal.msgError("该设备已被绑定");
                 }else{
                   //传参跳转到新增界面
                   thant.$tab.navigateTo('/devicelistpkg/deviceAdd/index?equipmentNo='+equipmentNo+'&equipmentTypeId='+equipmentTypeId+'&storeId='+thant.storeId)
                 }
               })
            }else{
               thant.$modal.msgError("MD5校验不通过");
            }
          }
        })
      }
    }
  }
</script>

<style>   
.root {
	padding: 16px 16px;
}
.homeinfo{
  text-align: center;font-size: 28rpx;color: gray;
}
.device-connect-title {
	margin-top: 8px;
  font-family: PingFangSC-Medium;
	font-size: 18px;
	letter-spacing: 0px;
	color: rgba(0, 0, 0, 0.9);
}

.device-connect-text {
	font-family: PingFangSC-Regular;
	font-size: 14px;
	color: rgba(0, 0, 0, 0.5);
}

.device-connect-item {
	padding: 28px 16px 28px 20px;
	display: flex;
	margin-top: 8px;
	background-color: #fff;
	border-radius: 16px;
}

.device-connect-item-name {
	flex: 1;
	font-family: PingFangSC-Regular;
	font-size: 16px;
	color: #22242c;
}

.device-connect-copy-title {
  font-family: PingFangSC-Regular;
	font-size: 16px;
	line-height: 24px;
	color: #22242c;
}
</style>
